<?php
declare(strict_types = 1);

namespace app\common\model\delivery;

use app\common\model\base\ConstTraits;
use app\common\service\msg\WechatMsg;
use think\facade\Db;
use think\Model;

class PayLog Extends Model
{
    use ConstTraits;

    const TYPE=[
        'saleOrder'=>'支付订单',
        'payYuefu'=>'支付月付账单',
        'shopOrder'=>'支付积分商城订单',
    ];

    protected $error;

    public function paySuccess($transaction_id)
    {
        $type=$this['type'];
        switch ($type){
            case 'saleOrder';
                return $this->saleOrder($transaction_id);
                break;
            case 'payYuefu';
                return $this->payYuefu($transaction_id);
                break;
            case 'shopOrder';
                return $this->payShop($transaction_id);
                break;
        }
    }


    private function payShop($transaction_id)
    {
        $r=false;
        Db::startTrans();
        try{
            $this->pay_status=1;
            $this->pay_time=date('Y-m-d H:i:s',time());
            $this->transaction_id=$transaction_id;
            $this->save();
            $order=ShopOrder::where(['order_no'=>$this->out_trade_no])->find();
            $order->is_pay=1;
            $order->pay_id=$this->id;
            $order->save();
            $goods=$order->goods;
            $goods->sale=$goods->sale+$order->number;
            if($goods->goods_type==ShopGoods::GOODSTYPE('实体商品')){
                $goods->stock=$goods->stock-$order->number;
            }
            $goods->save();
            if($goods->goods_type==ShopGoods::GOODSTYPE('卡券商品')){
                $coupon=Coupon::find($goods->coupon_id);
                CouponList::send($order->customer_id,$coupon,$order->number,'积分商城购买商品【'.$goods['name'].'】');
            }
            if($order->total_score>0){
                UserLog::addScoreLog($order->user_id,'minus',$order->total_score,$order->order_no,'积分商城购买商品【'.$goods['name'].'】积分抵扣');
            }
            if($order->total_money>=1){
                $addscore=intval($order->total_money);
                UserLog::addScoreLog($order->user_id,'add',$addscore,$order->order_no,'积分商城购买商品【'.$goods['name'].'】赠送积分');
            }
            Db::commit();
            $r=true;
        }catch (\Exception $e){
            $this->error=$e->getMessage();
            Db::rollback();
        }
        return $r;
    }

    private function saleOrder($transaction_id)
    {
        $r=false;
        Db::startTrans();
        try{
            $this->pay_status=1;
            $this->pay_time=date('Y-m-d H:i:s',time());
            $this->transaction_id=$transaction_id;
            $this->save();
            $order=ChannelOrder::where(['order_no'=>$this->out_trade_no])->find();
            $order->is_pay=1;
            $order->pay_id=$this->id;
            $order->save();
            if($order->hurry && $order->gird_id){
                $gird=ChannelGird::find($order->gird_id);
                if($gird->drivers){
                    $userid=Driver::whereIn('id',$gird->drivers)->column('user_id');
                    foreach ($userid as $id){
                        WechatMsg::hurryOrderDelivery($id,$order);
                    }
                }
            }
            if($order->coupon_money>0){
                $verify=CouponVerify::with(['coupon','couponlist'])->where(['order_table'=>'channel_order','order_id'=>$order->id])->find();
                $coupon=$verify->coupon;
                $couponlist=$verify->couponlist;
                $coupon->take=$coupon->take+1;
                $coupon->save();
                $couponlist->take=$couponlist->take+$order->number;
                $couponlist->save();
            }
            Db::commit();
            $r=true;
        }catch (\Exception $e){
            $this->error=$e->getMessage();
            Db::rollback();
        }
        if($r){
            WechatMsg::payOrderSuccess($order);
        }
        return $r;
    }

    private function payYuefu($transaction_id)
    {
        $order=MonthBill::where(['order_no'=>$this->out_trade_no])->find();
        $customer=Customer::where(['id'=>$order->customer_id])->find();
        $lastmonth=MonthBill::getLastMonthOrderDate($customer);
        $total_money=ChannelOrder::where(['customer_id'=>$order->customer_id,'goods_id'=>$order->goods_id,'is_cancel'=>0,'pay_type'=>'month','pay_id'=>null])->where('createtime','between',$lastmonth)->sum('total_money');
        if($total_money!=$order->total_money){
            $this->error='账单金额不正确';
            return false;
        };
        $r=false;
        Db::startTrans();
        try{
            $this->pay_status=1;
            $this->pay_time=date('Y-m-d H:i:s',time());
            $this->transaction_id=$transaction_id;
            $this->save();
            $order->is_pay=1;
            $order->pay_id=$this->id;
            $order->remark='微信支付';
            $order->save();
            ChannelOrder::where(['customer_id'=>$order->customer_id,'goods_id'=>$order->goods_id,'is_cancel'=>0,'pay_type'=>'month','pay_id'=>null])->where('createtime','between',$lastmonth)->update(['pay_id'=>$this->id]);
            Db::commit();
            $r=true;
        }catch (\Exception $e){
            $this->error=$e->getMessage();
            Db::rollback();
        }
        if($r){
            WechatMsg::payOrderSuccess($order);
        }
        return $r;
    }

    public function getError()
    {
        return $this->error;
    }
}
